﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace Controllerss
{
   public class MenuMemberController
   {
       #region Value
       private readonly MenuMemberBO _menuMemberBo=new MenuMemberBO();
       #endregion

       #region Ham lay du lieu

       public IQueryable<RelaxMenuMemberDataContract> GetMenuMemberDataContracts()
       {
           using (var scope = new TransactionScope())
           {
               var lReturn = _menuMemberBo.GetMenuMemberDataContracts();
               scope.Complete();
               return lReturn;
           }
       }
       #endregion

       #region Add

       public List<long> Add(List<RelaxMenuMemberDataContract> lmenuMemberDataContract)
       {
           using (var scope = new TransactionScope())
           {
               var liD = new List<long>();
               foreach (var item in lmenuMemberDataContract)
               {
                   var memberMenu = new RelaxMenu_ManagerMember
                   {
                       managermemberId = Convert.ToInt32(item.MemberId),
                       menuId = Convert.ToInt32(item.MenuId),
                       active = item.Active
                   };
                   _menuMemberBo.Add(memberMenu);

                   var first = _menuMemberBo.GetMenuMemberDataContracts().OrderByDescending(o => o.Id).FirstOrDefault();
                   if (first != null)
                       liD.Add(Convert.ToInt32(first.Id));
               }
               
               scope.Complete();
               return liD;
           }
       }

       #endregion

       #region Update
       public Boolean Update(RelaxMenuMemberDataContract menuMemberDataContract)
       {
           using (var scope = new TransactionScope())
           {
               
               var memberMenu = new RelaxMenu_ManagerMember
               {
                   id = Convert.ToInt32(menuMemberDataContract.Id),
                   managermemberId = Convert.ToInt32(menuMemberDataContract.MemberId),
                   menuId = Convert.ToInt32(menuMemberDataContract.MenuId),
                   active = menuMemberDataContract.Active
               };
               _menuMemberBo.Update(memberMenu);
               scope.Complete();
               return true;
           }
       }
       #endregion

       #region Delete

       public Boolean Delete(List<long> lId)
       {
           using (var scope = new TransactionScope())
           {
               foreach (var id in lId)
               {
                   _menuMemberBo.Delete(id);
               }
               scope.Complete();
               return true;
           }
       }
       #endregion
   }
}
